package com.tongji.housediy.model
{
	import alternativa.engine3d.containers.LODContainer;
	
	import com.tongji.housediy.events.commonEvent.ChangeData3D;
	import com.tongji.housediy.events.commonEvent.ExchangeDataEvent;
	import com.tongji.housediy.events.commonEvent.LoadAdminEvent;
	import com.tongji.housediy.utils.AbsorptionDetection;
	import com.tongji.housediy.utils.LoadAdmin;
	import com.tongji.housediy.utils.MathCalculation;
	import com.tongji.housediy.view.Room3DMediator;
	import com.tongji.housediy.view.component3D.StraightWall;
	import com.tongji.housediy.view.components.AbstractBorderChild;
	import com.tongji.housediy.view.components.AbstractDrawingRoom;
	import com.tongji.housediy.view.components.CommonWindow;
	import com.tongji.housediy.view.components.DoubleDoor;
	import com.tongji.housediy.view.components.DoubleShiftDoor;
	import com.tongji.housediy.view.components.DoubleWindow;
	import com.tongji.housediy.view.components.FixedWindow;
	import com.tongji.housediy.view.components.Furniture;
	import com.tongji.housediy.view.components.GridContainer;
	import com.tongji.housediy.view.components.IndependentFloor;
	import com.tongji.housediy.view.components.InteriorWall;
	import com.tongji.housediy.view.components.NormalDoor;
	import com.tongji.housediy.view.components.NormalWindow;
	import com.tongji.housediy.view.components.OpeningWall;
	import com.tongji.housediy.view.components.RoomBorder;
	import com.tongji.housediy.view.components.RoomFloor;
	import com.tongji.housediy.view.components.RoomShape;
	import com.tongji.housediy.view.components.ShiftDoor;
	import com.tongji.housediy.view.components.SingleDoor;
	import com.tongji.housediy.vo.Design;
	import com.tongji.housediy.vo.ExchangeData;
	import com.tongji.housediy.vo.Goods;
	import com.tongji.housediy.vo.PicModel;
	
	import de.polygonal.ds.DListNode;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.GraphicsBitmapFill;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.core.FlexGlobals;
	import mx.olap.aggregators.CountAggregator;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class ExchangeDataModel extends Actor
	{
		public var cameraPosition:Point;
		public var cameraRotation:Number;
		
		public var gridContainer:GridContainer;
		
		public var relatedOriCharacter:Image;
		public var relatedRoomVector:Vector.<Vector.<AbstractDrawingRoom>> = new Vector.<Vector.<AbstractDrawingRoom>>();
		public var relatedExchangeDataVector:Vector.<ExchangeData>;
		
		//caddy 3-28
		public var roomXML:XML = new XML();
		public var roomImg:BitmapData;
		public var designArray:ArrayCollection = new ArrayCollection();
		
		public var scrollValue:Number;
		[Inject]
		public var goodsModel:GoodsModel;
		
		public var isOpened:Boolean = false;
		public var srcArray:Array = new Array();
		public var dict:Dictionary = new Dictionary();
		public var current:int = 0;
		public function ExchangeDataModel()
		{
			super();
		}
		
		public function updateRelatedRoomData(dragedRoom:AbstractDrawingRoom, oppositedRoomVector:Vector.<AbstractDrawingRoom>):void
		{
			if (oppositedRoomVector != null)
			{
				for each (var relatedRoom:AbstractDrawingRoom in oppositedRoomVector)
				{
					var length:int = relatedRoomVector.length;
					
					// 首先查找 relatedRoomVector 里有没有一样的向量
					for (var i:int = 0; i < length; i++)
					{
						if (relatedRoomVector[i].indexOf(dragedRoom) != -1 && relatedRoomVector[i].indexOf(relatedRoom) != -1)
							break;
					}
					
					if (i >= length)
					{
						var vector:Vector.<AbstractDrawingRoom> = new Vector.<AbstractDrawingRoom>();
						vector.push(dragedRoom, relatedRoom);
						relatedRoomVector.push(vector);
					}
				}
			}
			else
			{
				// 删除带有 dragedRoom 的向量
				for (i = 0; i < relatedRoomVector.length; i++)
				{
					if (relatedRoomVector[i].indexOf(dragedRoom) != -1)
					{
						relatedRoomVector.splice(i, 1);
						i--;
					}
				}
			}
		}
		
		public function updateExchangeData():void
		{
			relatedExchangeDataVector = new Vector.<ExchangeData>();
			for (var i:int = 0; i < gridContainer.numChildren; i++)
			{
				if ((gridContainer.getChildAt(i) is AbstractDrawingRoom) && !(gridContainer.getChildAt(i) is IndependentFloor))
					relatedExchangeDataVector.push(getRoomData(gridContainer.getChildAt(i) as AbstractDrawingRoom));
			}
			
			dispatch(new ChangeData3D(ChangeData3D.CHANGE_DATA));
			dispatch(new ExchangeDataEvent(ExchangeDataEvent.UPDATE_MAP_GRAPHICS));
		}
		
		public function resetBorderChild():void
		{
			for (var i:int = 0; i < gridContainer.numChildren; i++)
			{
				if ((gridContainer.getChildAt(i) is AbstractDrawingRoom) && !(gridContainer.getChildAt(i) is IndependentFloor))
					deleteFakeData(gridContainer.getChildAt(i) as AbstractDrawingRoom);
			}
		}
		
		public function updateExchangeData1(host:AbstractDrawingRoom, oriCharacter:Image):void
		{
			cameraPosition = new Point(oriCharacter.x + host.x, oriCharacter.y + host.y);
			cameraRotation = Math.random() * 360;
			
			relatedExchangeDataVector = new Vector.<ExchangeData>();
			
			var roomContainer:Vector.<AbstractDrawingRoom> = new Vector.<AbstractDrawingRoom>();
			roomContainer.push(host);
			for (var index:int = 0; index < relatedRoomVector.length; index++)
			{
				if (relatedRoomVector[index].indexOf(host) != -1)
				{
					if (relatedRoomVector[index][0] != host)
						roomContainer.push(relatedRoomVector[index][0]);
					else
						roomContainer.push(relatedRoomVector[index][1]);
				}
			}
			
			for each (var room:AbstractDrawingRoom in roomContainer)
			{
				relatedExchangeDataVector.push(getRoomData(room));
			}
		}
		//////////////////////////////////////by zazisb 删除临时门窗
		private function deleteFakeData(host:AbstractDrawingRoom):void
		{
			var currentNode:DListNode = host.borderVertexList.head;
			
			while (currentNode.next != host.borderVertexList.head)
			{
				currentNode = currentNode.next;
				
				if (currentNode.data is RoomBorder)
				{
					for (var j:int = 0; j < (currentNode.data as RoomBorder).numChildren; j++)
					{
						if ((currentNode.data as RoomBorder).getChildAt(j) is AbstractBorderChild)
						{
							var borderChild:AbstractBorderChild = (currentNode.data as RoomBorder).getChildAt(j) as AbstractBorderChild;
							
							if ((borderChild is NormalDoor || borderChild is NormalWindow) && borderChild.fake)
								(currentNode.data as RoomBorder).removeChild(borderChild);
						}
					}
				}
			}
		}
		//////////////////////////////////////////////////////////////////////////
		
		private function getRoomData(host:AbstractDrawingRoom):ExchangeData
		{
			var exchangeData:ExchangeData = new ExchangeData();
			
			// 设置层高
			exchangeData.roomHeight = host.roomHeight;
			
			// 整理边墙的数据
			exchangeData.vertices = new Vector.<Number>();
			for (var i:int = 0; i < host.vertices.length; i++)
			{
				if ((i % 2) == 0)
					exchangeData.vertices.push(host.vertices[i] + host.x);
				else
					exchangeData.vertices.push(host.vertices[i] + host.y);
			}
			
			exchangeData.curveVector = host.curveVector;
			
			// 相关地板
			exchangeData.relatedFloor = host.roomFloor;
			
			exchangeData.boundingBox = host.getRect(host.parent);
			
			// 内墙 & 地板 数据结构 整理
			exchangeData.initialWallVector = new Vector.<Number>();
			for (i = 0; i < host.roomFloor.numChildren; i++)
			{
				if (host.roomFloor.getChildAt(i) is InteriorWall)
				{
					var wall:InteriorWall = host.roomFloor.getChildAt(i) as InteriorWall;
					exchangeData.initialWallVector.push(wall.borderVertexList.head.data.data[0] + wall.x + host.x,
						wall.borderVertexList.head.data.data[1] + wall.y + host.y,
						wall.borderVertexList.head.next.next.data.data[0] + wall.x + host.x,
						wall.borderVertexList.head.next.next.data.data[1] + wall.y + host.y);
				}
				else if (host.roomFloor.getChildAt(i) is IndependentFloor)
				{
					var floor:IndependentFloor = host.roomFloor.getChildAt(i) as IndependentFloor;
					floor.exchangeData = new Vector.<Number>();
					
					var rectangle:Rectangle = floor.getRect(floor.parent);
					
					for (var k:int = 0; k < floor.vertices.length; k += 2)
					{
						var tempPoint:Point = MathCalculation.calculateOffsetPoint(new Point(floor.vertices[k], floor.vertices[k + 1]), floor.rotation);
						floor.exchangeData.push(tempPoint.x + host.x, tempPoint.y + host.y);
					}
				}
			}
			
			// 门、窗、开放墙 数据结构 整理
			exchangeData.borderChildVector = new Vector.<Vector.<Number>>();
			
			// to do via Zhouliang
			////////////////////////////////////////////by zazisb 添加临时门 
			var currentNode:DListNode = host.borderVertexList.head;
			//遍历room的边找出共享边（被借走child的边） 
			while (currentNode.next != host.borderVertexList.head)
			{
				currentNode = currentNode.next;
				var border:RoomBorder = currentNode.data as RoomBorder;
				if (currentNode.data is RoomBorder)
				{
					
					//转换坐标为网格坐标
					var lendChildBorderPoint1:Point;
					var lendChildBorderPoint2:Point;
					
					lendChildBorderPoint1 = MathCalculation.transExPtFromInPt(border, new Point(border.data[0], border.data[1]));
					lendChildBorderPoint2 = MathCalculation.transExPtFromInPt(border, new Point(border.data[2], border.data[3]));
					
					if(border.absorbRelatedBorder.length > 0)
					{
						//遍历借走child的边， 找出其中shared的值为true且ownedBorder为被借走边的child
						for(var l:int = 0; l < border.absorbRelatedBorder.length; l++)
						{
							var ownedChildArray:Array = new Array();
							
							ownedChildArray = (border.absorbRelatedBorder[l] as RoomBorder).findChildShared(border);
							//将这些child添加到被借走child的边上
							if(ownedChildArray.length > 0)
							{
								for(var m:int = 0; m < ownedChildArray.length; m++)
								{
									//借走child边上的borderchild做垂足到被借走的边上
									var lendChildBorderVec:Vector.<Number> = new Vector.<Number>;
									
									lendChildBorderVec.push(lendChildBorderPoint1.x);
									lendChildBorderVec.push(lendChildBorderPoint1.y);
									lendChildBorderVec.push(lendChildBorderPoint2.x);
									lendChildBorderVec.push(lendChildBorderPoint2.y);
									
									var sharedChildPoint1:Point = MathCalculation.transExPtFromInPt(ownedChildArray[m], new Point(ownedChildArray[m].data[0], ownedChildArray[m].data[1]));
									//var sharedChildPoint2:Point = MathCalculation.transExPtFromInPt(ownedChildArray[j], new Point(ownedChildArray[j].data[2], ownedChildArray[j].data[3]));
									
									var childToLendBorderPoint1:Point = MathCalculation.getPedalPoint(border.k, lendChildBorderVec, new Point(sharedChildPoint1.x, sharedChildPoint1.y));
									//var childToLendBorderPoint2:Point = MathCalculation.getPedalPoint(border.k, lendChildBorderVec, new Point(sharedChildPoint2.x, sharedChildPoint2.y));
									
									//将它们转换成被借走孩子边上的坐标
									var exPt1:Point = MathCalculation.transInPtFromExPt(border, childToLendBorderPoint1);
									
									//在高index的边上加上Borderchild
									var tmpPoint:Point = MathCalculation.getOffsetValue(border.k, ownedChildArray[m].length, border.data);//解决门移动后的偏移
									
									exPt1.x -= tmpPoint.x;
									exPt1.y -= tmpPoint.y;
									
									if(ownedChildArray[m] is NormalDoor)
									{
										var tmpDoor:NormalDoor;
										
										if (ownedChildArray[m] is SingleDoor)
										{
											tmpDoor = new SingleDoor((ownedChildArray[m] as SingleDoor).length,
												(ownedChildArray[m] as SingleDoor).map.clone(), (ownedChildArray[m] as SingleDoor).hostGoods);
										}
										else if (ownedChildArray[m] is DoubleDoor)
										{
											tmpDoor = new DoubleDoor((ownedChildArray[m] as DoubleDoor).length,
												(ownedChildArray[m] as DoubleDoor).map.clone(), (ownedChildArray[m] as DoubleDoor).hostGoods);
										}
										else if (ownedChildArray[m] is ShiftDoor)
										{
											tmpDoor = new ShiftDoor((ownedChildArray[m] as ShiftDoor).length,
												(ownedChildArray[m] as ShiftDoor).map.clone(), (ownedChildArray[m] as ShiftDoor).hostGoods);
										}
										else if (ownedChildArray[m] is DoubleShiftDoor)
										{
											tmpDoor = new DoubleShiftDoor((ownedChildArray[m] as DoubleShiftDoor).length,
												(ownedChildArray[m] as DoubleShiftDoor).map.clone(), (ownedChildArray[m] as DoubleShiftDoor).hostGoods);
										}
										border.addChild(tmpDoor);
										tmpDoor.parentBorder = border;
										
										
										tmpDoor.x = exPt1.x;
										tmpDoor.y = exPt1.y;
										
										tmpDoor.rotateState = ownedChildArray[m].rotateState;
										
										if(tmpDoor.rotateState == 1 || tmpDoor.rotateState == 2)
										{
											tmpDoor.x += tmpPoint.x;
											tmpDoor.y += tmpPoint.y;
										}
										else
										{
											tmpDoor.x -= tmpPoint.x;
											tmpDoor.y -= tmpPoint.y;
										}
										
										tmpDoor.setRotation(border.data, border.k, exPt1);
										tmpDoor.percentPosition = Math.sqrt((exPt1.x - border.data[0])  
											* (exPt1.x - border.data[0]) + (exPt1.y - border.data[1]) * (exPt1.y - border.data[1]))
											/ Math.sqrt((border.data[2] - border.data[0]) * (border.data[2] - border.data[0])
												+ (border.data[3] - border.data[1]) * (border.data[3] - border.data[1]));
										
										border.changeChildIndex(tmpDoor);
										
										//var tmpPoint = MathCalculation.getOffsetValue(roomIndexHighBorder.k, 50, roomIndexHighBorder.data);
										var revRotate:Number = ownedChildArray[m].getReverseRotation(ownedChildArray[m].data, border.k);
										//转换相应的门状态
										
										tmpDoor.transformNormalDoorState(tmpPoint, revRotate, tmpDoor.rotateState);
										
										tmpDoor.rotateState += 2;
										
										if(tmpDoor.rotateState == 5)
											tmpDoor.rotateState = 1;
										else if(tmpDoor.rotateState == 6)
											tmpDoor.rotateState = 2;
										
										tmpDoor.fake = true;
										tmpDoor.sharedBorder = border.absorbRelatedBorder[l] as RoomBorder;
									}
									else if(ownedChildArray[m] is NormalWindow)
									{
										var tmpWindow:NormalWindow;
										if (ownedChildArray[m] is CommonWindow)
										{
											tmpWindow = new CommonWindow((ownedChildArray[m] as CommonWindow).length,
												(ownedChildArray[m] as CommonWindow).map.clone(), (ownedChildArray[m] as CommonWindow).hostGoods);
										}
										else if (ownedChildArray[m] is CommonWindow)
										{
											tmpWindow = new FixedWindow((ownedChildArray[m] as FixedWindow).length,
												(ownedChildArray[m] as FixedWindow).map.clone(), (ownedChildArray[m] as FixedWindow).hostGoods);
										}
										else if (ownedChildArray[m] is DoubleWindow)
										{
											tmpWindow = new DoubleWindow((ownedChildArray[m] as DoubleWindow).length,
												(ownedChildArray[m] as DoubleWindow).map.clone(), (ownedChildArray[m] as DoubleWindow).hostGoods);
										}
										
										border.addChild(tmpWindow);
										tmpWindow.parentBorder = border;
										
										
										tmpWindow.x = exPt1.x;
										tmpWindow.y = exPt1.y;
										
										tmpWindow.setRotation(border.data, border.k, exPt1);
										tmpWindow.percentPosition = Math.sqrt((exPt1.x - border.data[0])  
											* (exPt1.x - border.data[0]) + (exPt1.y - border.data[1]) * (exPt1.y - border.data[1]))
											/ Math.sqrt((border.data[2] - border.data[0]) * (border.data[2] - border.data[0])
												+ (border.data[3] - border.data[1]) * (border.data[3] - border.data[1]));
										
										border.changeChildIndex(tmpWindow);
										
										tmpWindow.fake = true;
										tmpWindow.sharedBorder = border.absorbRelatedBorder[l] as RoomBorder;
									}
								}
							}
						}
					}
				}
			}
			///////////////////////////////////////////////////////////////////////////////////////////////
			
			exchangeData.borderVector = new Vector.<RoomBorder>();
			
			exchangeData.doorWindowRelatedMap = new Vector.<BitmapData>();
			
			currentNode= host.borderVertexList.head;
			
			while (currentNode.next != host.borderVertexList.head)
			{
				currentNode = currentNode.next;
				if (currentNode.data is RoomBorder)
				{
					exchangeData.borderVector.push(currentNode.data as RoomBorder);
					
					var borderChildVector:Vector.<Number> = new Vector.<Number>();
					
					for (var j:int = 0; j < (currentNode.data as RoomBorder).numChildren; j++)
					{
						if ((currentNode.data as RoomBorder).getChildAt(j) is AbstractBorderChild)
						{
							var borderChild:AbstractBorderChild = (currentNode.data as RoomBorder).getChildAt(j) as AbstractBorderChild;
							borderChildVector.push(borderChild.data[0] + host.x, -borderChild.data[1] - host.y, borderChild.data[2] + host.x, -borderChild.data[3] - host.y);
							
							var isTwoSide:int = 0;
							
							if ((currentNode.data as RoomBorder).getChildAt(j) is NormalDoor)
							{
								isTwoSide = 0;
								if (((currentNode.data as RoomBorder).getChildAt(j) as NormalDoor).shared || ((currentNode.data as RoomBorder).getChildAt(j) as NormalDoor).borrowed || ((currentNode.data as RoomBorder).getChildAt(j) as NormalDoor).fake)
									isTwoSide = 1;							
								borderChildVector.push(1, ((currentNode.data as RoomBorder).getChildAt(j) as NormalDoor).map.height, 0, isTwoSide);
								
								
								///////////////////////////by zazisb
								/*if(borderChild.borrowed || borderChild.shared || borderChild.fake)
								{
								exchangeData.doorRelatedRoomVector.push(borderChild.sharedBorder.parent as AbstractDrawingRoom);
								}
								else
								{
								exchangeData.doorRelatedRoomVector.push(null);
								}*/
								////////////////////////////////////////////////
								
								exchangeData.doorWindowRelatedMap.push(((currentNode.data as RoomBorder).getChildAt(j) as NormalDoor).map);
							}
							else if ((currentNode.data as RoomBorder).getChildAt(j) is NormalWindow)
							{
								borderChildVector.push(100, ((currentNode.data as RoomBorder).getChildAt(j) as NormalWindow).map.height + 100, 1, -1);
								
								exchangeData.doorWindowRelatedMap.push(((currentNode.data as RoomBorder).getChildAt(j) as NormalWindow).map);
							}
							else if ((currentNode.data as RoomBorder).getChildAt(j) is OpeningWall)
							{
								borderChildVector.push(1, exchangeData.roomHeight/10 - 25, 2, -1);
							}
						}
					}
					
					exchangeData.borderChildVector.push(borderChildVector);
				}
			}
			
			return exchangeData;
		}
		
		public function openDesignResult(roomXML:XML):void
		{
			this.roomXML = roomXML;
		}
		
		public function saveHouse():XML
		{
			var roomsXML:XML = 
				<rooms time="" name="">
				</rooms>
			// 添加房间
			for(var i:int = 0; i < gridContainer.numChildren; i++)
			{				  
				if(gridContainer.getChildAt(i) is AbstractDrawingRoom)
				{
					var room:AbstractDrawingRoom = gridContainer.getChildAt(i) as AbstractDrawingRoom;
					gridContainer.getChildIndex(room);
					var roomXML:XML = 
						<room name="" index="" x="" y="" vertices="" curveVector="" borderCount="" borderVertextList="" roomType="">								
						</room>				
					roomXML.@index = gridContainer.getChildIndex(room);	
					roomXML.@name = room.roomName;
					roomXML.@x = room.x;
					roomXML.@y = room.y;
					roomXML.@height = room.roomHeight;
					roomXML.@vertices = room.vertices;
					roomXML.@borderCount = room.borderLength;
					roomXML.@curveVector = room.curveVector;
					roomXML.@borderVertextList = room.borderVertexList;
					roomXML.@roomType = "room";
					roomXML.@curveVector = room.curveVector;
					roomsXML.appendChild(roomXML);
					
					for(var a:int = 0; a < room.numChildren; a++)
					{	
						// 添加地板													
						if(room.getChildAt(a) is RoomFloor)
						{
							var roomFloor:RoomFloor = room.getChildAt(a) as RoomFloor;
							var floorXML:XML =
								<floor x="" y="" vertices="" borderCount="" goodsID="" modelID="" colorID="" mapSrc="">
								</floor>							
							floorXML.@x = roomFloor.x;
							floorXML.@y = roomFloor.y;
							//roomsXML.room[i].floor[a].@vertices = roomFloor.;
							floorXML.@borderCount = roomFloor.borderLength;
							floorXML.@goodsID = roomFloor.hostGoods.goodsID;
							floorXML.@modelID = roomFloor.selectedSizeID;
							floorXML.@colorID = roomFloor.selectedColorID;
							floorXML.@mapSrc = roomFloor.hostGoods.dragPicSrc;
							
							roomsXML.room[i].appendChild(floorXML);
							
							for(var b:int = 0; b < roomFloor.numChildren; b++)
							{																						
								// 添加家具
								if(roomFloor.getChildAt(b) is Furniture)
								{									
									var furniture:Furniture = roomFloor.getChildAt(b) as Furniture;
									var furnitureXML:XML =
										<floorChild type="furniture" x="" y="" rotation="" goodsID="" modelID="" colorID="" dragPicSrc="">
										</floorChild>
									
									furnitureXML.@rotation = furniture.rotation;
									furnitureXML.@x = furniture.x;
									furnitureXML.@y = furniture.y;
									furnitureXML.@goodsID = furniture.hostGoods.goodsID;
									furnitureXML.@modelID = furniture.selectedSizeID;
									furnitureXML.@colorID = furniture.selectedColorID;
									furnitureXML.@dragPicSrc = furniture.hostGoods.dragPicSrc;
									
									roomsXML.room[i].floor[a].appendChild(furnitureXML);
								}
									// 添加自定义地板
								else if(roomFloor.getChildAt(b) is IndependentFloor)
								{
									var independentFloor:IndependentFloor = roomFloor.getChildAt(b) as IndependentFloor;
									var independentFloorXML:XML =
										<floorChild type="independentFloor" x="" y="" vertices="" borderCount="" goodsID="" sizeID="" colorID="">
										</floorChild>
									
									independentFloorXML.@x = independentFloor.x;
									independentFloorXML.@y = independentFloor.y;
									independentFloorXML.@vertices = independentFloor.vertices;
									independentFloorXML.@borderCount = independentFloor.borderLength;
									independentFloorXML.@goodsID = independentFloor.roomFloor.hostGoods.goodsID;
									independentFloorXML.@sizeID = independentFloor.roomFloor.selectedSizeID;
									independentFloorXML.@colorID = independentFloor.roomFloor.selectedColorID;
									
									roomsXML.room[i].floor[a].appendChild(independentFloorXML);
								}
									// 添加内墙
								else if(roomFloor.getChildAt(b) is InteriorWall)
								{
									var interiorWall:InteriorWall = roomFloor.getChildAt(b) as InteriorWall;
									var interiorWallXML:XML = <floorChild type="interiorWall" x="" y="" vertices="" goodsID="" sizeID="" colorID="" skirtingGoodsID="" skirtingSizeID="" skirtingcolorID="">
										</floorChild>
									/*<interiorWall x="" y="" vertices="" goodsID="" sizeID="" colorID="" skirtingGoodsID="" skirtingSizeID="" skirtingcolorID="">
									</interiorWall>*/
									
									interiorWallXML.@x = interiorWall.x;
									interiorWallXML.@y = interiorWall.y;
									interiorWallXML.@vertices = (interiorWall.getChildAt(0) as RoomBorder).data;
									interiorWall.getChildAt(0);
									roomsXML.room[i].floor[a].appendChild(interiorWallXML);
								}
							}
						}						
						else if(room.getChildAt(a) is RoomBorder)
						{							
							var roomBorderCount:int;					
							var xml:XML = 
								<roomBorder index="" x="" y="" vertices="" goodsID="" 
										sizeID="" colorID="" roomType=""  absorbRelatedBorder="" >                                  
								</roomBorder>
							
							var roomBorder:RoomBorder = room.getChildAt(a) as RoomBorder;
							xml.@vertices = roomBorder.data;
							xml.@x = roomBorder.x;
							xml.@y = roomBorder.y;
							xml.@goodsID = roomBorder.hostGoods.goodsID;
							xml.@colorID = roomBorder.selectedColorID;
							xml.@sizeID = roomBorder.selectedSizeID;
							xml.@roomType = "room";
							xml.@index = roomBorder.index;
							
							roomsXML.room[i].appendChild(xml);
							//var borderIndex:int = roomsXML.room[i].child("roomBorder").length();
							var borderIndex:int = roomsXML.room[i].roomBorder.length();
							
							for(var m:int = 0; m < roomBorder.absorbRelatedBorder.length; m++)
							{
								var absorbRelatedBorderXML:XML =
									<borderChild  type="absorbRelatedBorder" vertices="" absorbIndex="" roomType="room" roomIndex=""/>
								var absorbBorder:RoomBorder = roomBorder.absorbRelatedBorder[m] as RoomBorder;
								absorbRelatedBorderXML.@vertices = absorbBorder.data;
								absorbRelatedBorderXML.@absorbIndex = roomBorder.absorbRelatedBorder[m].index;
								absorbRelatedBorderXML.@roomIndex = gridContainer.getChildIndex(roomBorder.absorbRelatedBorder[m].parent);
								roomsXML.room[i].roomBorder[borderIndex-1].appendChild(absorbRelatedBorderXML);
							}
							
							
							
							// 添加墙体上的孩子，门、窗、开放墙
							for(var l:int = 0; l < roomBorder.numChildren; l++)
							{
								// 添加墙上的门
								if(roomBorder.getChildAt(l) is NormalDoor)
								{
									var door:NormalDoor = roomBorder.getChildAt(l) as NormalDoor;
									var doorXML:XML =
										<borderChild x="" y="" goodsID="" sizeID=""
												colorID="" type="door" mapSrc="" 
												typeIndex="" parentIndex="" index="" 
												 length="" rotation="" data="" percentPosition="" 
												 shared="" rotateState="" segFlag="" borrowed="">
										</borderChild>
									doorXML.@segFlag = door.segFlag;
									doorXML.@parentIndex = roomBorder.index;
									doorXML.@x = door.x;
									doorXML.@y = door.y;
									doorXML.@goodsID = door.hostGoods.goodsID;
									doorXML.@sizeID = door.selectedSizeID;
									doorXML.@colorID = door.selectedColorID;
									doorXML.@typeIndex = door.typeIndex;
									doorXML.@index = door.index;
									doorXML.@length = door.length;
									doorXML.@rotation = door.rotation;
									doorXML.@rotateState= door.rotateState;
									doorXML.@data = door.data;
									doorXML.@percentPosition = door.percentPosition;
									doorXML.@mapSrc = door.hostGoods.dragPicSrc;
									doorXML.@shared = door.shared;
									doorXML.@borrowed = door.borrowed;
									
									roomsXML.room[i].roomBorder[borderIndex-1].appendChild(doorXML);
								}								
									// 添加墙上的窗
								else if(roomBorder.getChildAt(l) is NormalWindow)
								{
									var window:NormalWindow = roomBorder.getChildAt(l) as NormalWindow;
									var windowXML:XML =
										<borderChild x="" y="" goodsID="" sizeID="" colorID="" type="window" typeIndex="" parentIndex="" length="" rotation="" data="" percentPosition="">
										</borderChild>
									
									windowXML.@parentIndex = roomBorder.index;
									windowXML.@x = window.x;
									windowXML.@y = window.y;
									windowXML.@goodsID = window.hostGoods.goodsID;
									windowXML.@sizeID = window.selectedSizeID;
									windowXML.@colorID = window.selectedColorID;
									windowXML.@typeIndex = window.typeIndex;
									windowXML.@length = window.length;
									windowXML.@rotation = window.rotation;
									windowXML.@data = window.data;
									windowXML.@percentPosition = window.percentPosition;
									
									roomsXML.room[i].roomBorder[borderIndex-1].appendChild(windowXML);
								}								
									// 添加墙上的开放墙
								else if(roomBorder.getChildAt(l) is OpeningWall)
								{				
									var openingWall:OpeningWall = roomBorder.getChildAt(l) as OpeningWall;
									var openingWallXML:XML =
										<borderChild x="" y="" type="openingWall" typeIndex="" parentIndex="" length="" rotation="" data="" percentPosition="">
										</borderChild>
									
									openingWallXML.@parentIndex = roomBorder.index;
									openingWallXML.@x = openingWall.x;
									openingWallXML.@y = openingWall.y;
									openingWallXML.@length = openingWall.length;
									openingWallXML.@data = openingWall.data;
									openingWallXML.@percentPosition = openingWall.percentPosition;
									openingWallXML.@rotation = openingWall.rotation;
									//openingWallXML.@vertices = openingWall.
									roomsXML.room[i].roomBorder[borderIndex-1].appendChild(openingWallXML);	
								}
							}
						}	
					}							
				}
			}
			return roomsXML;
		}
		
		
		public function openHouse(xml:XML):void
		{
			roomXML = xml;
			srcArray = new Array();
			var roomCount:int = xml.room.length();
			for (var a:int = 0; a < roomCount; a++)
			{
				var roomXML:XML = xml.room[a];
				var floorXML:XML = roomXML.floor[0];
				srcArray.push(floorXML.@mapSrc.toString());
				
				for (var c:int = 0; c < roomXML.roomBorder.length(); c++)
				{
					srcArray.push(goodsModel.getWallGoodsByID(int(roomXML.roomBorder[c].@goodsID)).dragPicSrc);
				}
				for (var b:int = 0; b < floorXML.floorChild.length(); b++)
				{
					var floorChildXML:XML = floorXML.floorChild[b];
					
					switch (String(floorChildXML.@type))
					{
						case "furniture":
							srcArray.push(goodsModel.getFurnitureGoodsByID(int(floorChildXML.@goodsID)).dragPicSrc);
							break;
						case "interiorWall":
							break;
						case "independentFloor":
							srcArray.push(goodsModel.getFloorGoodsByID(int(floorChildXML.@goodsID)).mapPicSrc);
							break;
						default:break;
					}
				}
				for (var q:int = 0; q < int(roomXML.@borderCount); q++)
				{
					var borderChildCount:int = roomXML.roomBorder[q].borderChild.length();
					for (var p:int = 0; p < borderChildCount; p++)
					{
						var borderChildXML:XML = roomXML.roomBorder[q].borderChild[p];						
						switch (String(borderChildXML.@type))
						{
							case "door":
								srcArray.push(goodsModel.getDoorGoodsByID(int(borderChildXML.@goodsID)).mapPicSrc);
								break;
							case "window":
								srcArray.push(goodsModel.getWindowGoodsByID(int(borderChildXML.@goodsID)).mapPicSrc);
								break;
							default:break;
						}
					}					
				}
			}
			getBitmap(srcArray);
		}
		
		public function getBitmap(srcArray:Array):void
		{
			current = 0;
			dict = new Dictionary();
			for each(var src:String in srcArray)
			{
				
				var loader:Loader = new Loader();
				loader.load(new URLRequest(src));
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
			}
		}
		
		protected function onCompleteHandler(event:Event):void
		{
			var srcCount:int = srcArray.length;
			current++;
			dict[event.target.url] = event.target.content;
			if (current == srcCount)	
				house();			
		}
		protected function house():void
		{
			var roomCount:int = gridContainer.numChildren;
			if(gridContainer.numChildren > 0)
			{
				for(var t:int; t < roomCount; t++)
				{
					gridContainer.removeChildAt(gridContainer.numChildren - 1);
				}
			}
			var rooms:int = roomXML.room.length();
			var borderArray:ArrayCollection = new ArrayCollection();	
			for (var a:int = 0; a < rooms; a++)
			{
				
				var room:AbstractDrawingRoom = new AbstractDrawingRoom();
				var currentRoomXML:XML = roomXML.room[a];
				// 顶点赋值
				var tempString:String = currentRoomXML.@vertices.toString();				
				var arr:Array = tempString.split(",", tempString.length) as Array;				
				room._vertices = new Vector.<Number>();										
				for(var c:int = 0; c < arr.length; c++)
				{
					room._vertices.push(arr[c]);
				}
				
				// curveVector 赋值
				var tempString2:String = currentRoomXML.@curveVector.toString();				
				var arr2:Array = tempString2.split(",", tempString.length) as Array;
				if (tempString2 == "")
					arr2 = new Array();
				room.curveVector = new Vector.<int>();
				for(var j:int = 0; j < arr2.length; j++)
				{
					room.curveVector.push(arr2[j]);
				}
				
				// x, y, borderLength, height, roomType 属性赋值
				room.x = currentRoomXML.@x;
				room.y = currentRoomXML.@y;
				room.borderLength = currentRoomXML.@borderCount.toString();
				room.height = currentRoomXML.@height;				
				room.roomType = "room";
				
				//添加房间
				room.drawing();
				/*gridContainer.addChild(room);*/
				gridContainer.addChildAt(room, int(currentRoomXML.@index));
				/*gridContainer.setChildIndex(room, int(currentRoomXML.@index));*/
				room.parentGridContainer = gridContainer;
				for (var a1:int = 0; a1 < room.numChildren; a1++)
				{
					if (room.getChildAt(a1) is RoomBorder)
					{
						var currentBorder:RoomBorder = room.getChildAt(a1) as RoomBorder;
						for (var a2:int = 0; a2 < currentRoomXML.roomBorder.length(); a2++)
						{
							if(currentBorder.index == currentRoomXML.roomBorder[a2].@index)
							{
								var wallGoods:Goods = goodsModel.getWallGoodsByID(int(currentRoomXML.roomBorder[a2].@goodsID));								
								var picmodel:PicModel = new PicModel();
								picmodel.dragPicSrc = wallGoods.dragPicSrc;
								picmodel.mapPicSrc = wallGoods.mapPicSrc;
								picmodel.model = wallGoods.models[0];
								picmodel.selectedColorID = int(currentRoomXML.roomBorder[a2].@colorID);
								picmodel.selectedSizeID = int(currentRoomXML.roomBorder[a2].@sizeID);
								
								currentBorder.changeSizeAndColor(picmodel, dict[wallGoods.dragPicSrc].bitmapData, wallGoods);
							}
						}
					}
				}
				//添加地板
				var floorGoods:Goods = goodsModel.getFloorGoodsByID(int(currentRoomXML.floor[0].@goodsID));
				room.roomFloor.materialBitmapData = dict[floorGoods.dragPicSrc].bitmapData;
				room.roomFloor.stroke = new GraphicsBitmapFill(room.roomFloor.materialBitmapData);
				room.roomFloor.hostGoods = floorGoods;
				room.roomFloor.selectedColorID = int(currentRoomXML.floor[0].@colorID);
				room.roomFloor.selectedSizeID = int(currentRoomXML.floor[0].@modelID);
				room.roomFloor.dragPicSrc = currentRoomXML.floor[0].@mapSrc as String;
				//重绘地板，更改材质
				room.roomFloor.reDraw(room.borderLength);
				
				//添加地板上面的家具
				var floorChildLength:int = currentRoomXML..floorChild.length();
				for (var d:int = 0; d < floorChildLength; d++)
				{
					var floorChildXML:XML = currentRoomXML..floorChild[d];
					//添加家具
					if (String(floorChildXML.@type) == "furniture")
					{
						var furnitureGoods:Goods = goodsModel.getFurnitureGoodsByID(int(floorChildXML.@goodsID));
						var furniture:Furniture = new Furniture(furnitureGoods, dict[furnitureGoods.dragPicSrc].bitmapData);
						furniture.x = Number(floorChildXML.@x);
						furniture.y = Number(floorChildXML.@y);
						furniture.rotation = Number(floorChildXML.@rotation);
						room.roomFloor.addChild(furniture);
					}
						//添加独立地板
					else if(String(floorChildXML.@type) == "independentFloor")
					{
						var independentFloor:IndependentFloor = new IndependentFloor();	
						var tempGoods:Goods = goodsModel.getFloorGoodsByID(int(floorChildXML.@goodsID));
						var tempString3:String = floorChildXML.@vertices.toString();				
						var arr3:Array = tempString3.split(",", tempString3.length) as Array;
						if (tempString3 == "")
							arr3 = new Array();
						independentFloor._vertices = new Vector.<Number>();
						for(var k:int = 0; k < arr3.length; k++)
						{
							independentFloor._vertices.push(arr3[k]);
						}
						independentFloor.x = floorChildXML.@x;
						independentFloor.y = floorChildXML.@y;
						independentFloor.roomFloor.materialBitmapData = dict[tempGoods.mapPicSrc].bitmapData;
						independentFloor.roomFloor.stroke = new GraphicsBitmapFill(independentFloor.roomFloor.materialBitmapData);
						independentFloor.roomFloor.reDraw(int(floorChildXML.@borderCount));
						room.roomFloor.addChild(independentFloor);
						independentFloor.parentGridContainer = gridContainer;
					}
						////添加实体墙
					else if(String(floorChildXML.@type) == "interiorWall")
					{
						
						var tempString4:String = floorChildXML.@vertices.toString();				
						var arr4:Array = tempString4.split(",", tempString4.length) as Array;
						if (tempString4 == "")
							arr4 = new Array();
						var temp:Vector.<Number> = new Vector.<Number>();
						
						for(var t2:int = 0; t2 < arr4.length; t2++)
						{
							temp.push(arr4[t2]);
						}
						var interiorWall:InteriorWall = new InteriorWall(temp);
						interiorWall.x = floorChildXML.@x;
						interiorWall.y = floorChildXML.@y;
						room.roomFloor.addChild(interiorWall);	
						interiorWall.parentGridContainer = gridContainer;
						
					}
				}
				
				var borders:ArrayCollection = new ArrayCollection();
				var newborder:Object = new Object();
				newborder.room = a;
				newborder.borders = borders;
				borderArray.addItem(newborder);
				// 添加 门，窗
				var currentNode:DListNode = room.borderVertexList.head;
				var roomBorder:RoomBorder = currentNode.next.data as RoomBorder;
				// 先移位一次
				currentNode = currentNode.next;
				roomBorder.absorbRelatedBorder = new Array();
				
				
				var roomBorderLength:int = currentRoomXML.roomBorder.length();
				var aborBorder:RoomBorder;
			}
			for (var t1:int = 0; t1 < roomXML.room.length(); t1++)
			{
				var currentRoom:AbstractDrawingRoom = gridContainer.getChildAt(int(roomXML.room[t1].@index)) as AbstractDrawingRoom;
				var currentRoomXML2:XML = roomXML.room[t1];
				for (var a3:int = 0; a3 < currentRoom.numChildren; a3++)
				{
					if (currentRoom.getChildAt(a3) is RoomBorder)
					{
						var currentBorder2:RoomBorder = currentRoom.getChildAt(a3) as RoomBorder;
						currentBorder2.absorbRelatedBorder = new Array();
						for (var a4:int = 0; a4 < currentRoomXML2.roomBorder.length(); a4++)
						{
							if(currentBorder2.index == currentRoomXML2.roomBorder[a4].@index)
							{
								var borderXML:XML = currentRoomXML2.roomBorder[a4];
								for (var m:int = 0; m < borderXML.children().length(); m++)
								{
									var borderChildXML:XML = borderXML.children()[m];
									var borderChild:AbstractBorderChild;
									if(borderChildXML.@type.toString() == "window")
									{							
										var windowHostGoods:Goods = goodsModel.getWindowGoodsByID(int(borderChildXML.@goodsID));
										var bitmap:BitmapData = dict[windowHostGoods.mapPicSrc].bitmapData;
										if (borderChildXML.@typeIndex.toString() == "0")
										{
											borderChild = new FixedWindow(Number(borderChildXML.@length), bitmap, windowHostGoods);
										}
										else if (borderChildXML.@typeIndex.toString() == "1")
										{
											borderChild = new DoubleWindow(Number(borderChildXML.@length), bitmap, windowHostGoods);
										}
										else if (borderChildXML.@typeIndex.toString() == "2")
										{
											borderChild = new CommonWindow(Number(borderChildXML.@length), bitmap, windowHostGoods);
										}
										currentBorder2.addChild(borderChild);
										//borderArray[windowArray[0]].borders[windowArray[1]].addChild(borderChild);
										borderChild.x = Number(borderChildXML.@x);
										borderChild.y = Number(borderChildXML.@y);
										borderChild.rotation = Number(borderChildXML.@rotation);
										borderChild.percentPosition = Number(borderChildXML.@percentPosition);
										
										// 整理顶点 data 数据
										var dataString:String = borderChildXML.@data.toString();				
										var dataArray:Array = dataString.split(",", dataString.length) as Array;				
										borderChild.data = new Vector.<Number>();										
										for(var m1:int = 0; m1 < dataArray.length; m1++)
										{
											borderChild.data.push(dataArray[m1]);
										}
										
										// 必须手动设置 父边框，否则无法响应鼠标事件
										//borderChild.parentBorder = borderArray[windowArray[0]].borders[windowArray[1]];
										borderChild.parentBorder = currentBorder2;
										AbsorptionDetection.DoAbsorption(borderChild.parentBorder, true, borderChild);
									}
									else if(borderChildXML.@type.toString() == "door")
									{							
										var doorHostGoods:Goods = goodsModel.getDoorGoodsByID(int(borderChildXML.@goodsID));
										var doorBitmap:BitmapData = dict[doorHostGoods.mapPicSrc].bitmapData;
										
										if (borderChildXML.@typeIndex.toString() == "0")
										{
											borderChild = new SingleDoor(Number(borderChildXML.@length),  doorBitmap, doorHostGoods);
										}
											// 新建 ShiftDoor
										else if (borderChildXML.@typeIndex.toString() == "1")
										{
											borderChild = new ShiftDoor(Number(borderChildXML.@length),  doorBitmap, doorHostGoods);
										}
											// 新建 DoubleShiftDoor
										else if (borderChildXML.@typeIndex.toString() == "2")
										{
											borderChild = new DoubleShiftDoor(Number(borderChildXML.@length),  doorBitmap, doorHostGoods);
										}
											// 新建 DoubleDoor
										else if (borderChildXML.@typeIndex.toString() == "3")
										{
											borderChild = new DoubleDoor(Number(borderChildXML.@length),  doorBitmap, doorHostGoods);
										}										
										currentBorder2.addChild(borderChild);
										
										
										borderChild.segFlag = borderChildXML.@segFlag as Boolean;
										borderChild.borrowed = borderChildXML.@borrowed as Boolean;
										borderChild.rotateState = int(borderChildXML.@rotateState);
										borderChild.rotation = Number(borderChildXML.@rotation);
										borderChild.percentPosition = Number(borderChildXML.@percentPosition);
										borderChild.shared = Boolean(borderChildXML.@shared)
										borderChild.index = int(borderChildXML.@index);
										var dataString1:String = borderChildXML.@data.toString();				
										var dataArray1:Array = dataString1.split(",", dataString1.length) as Array;				
										borderChild.data = new Vector.<Number>();										
										for(var m2:int = 0; m2 < dataArray1.length; m2++)
										{
											borderChild.data.push(dataArray1[m2]);
										}
										
										var segPoint:Point = MathCalculation.getOffsetValue(currentBorder2.k, borderChild.length, currentBorder2.data);
										var rotate:Number = borderChild.getReverseRotation(borderChild.data, currentBorder2.k);
										borderChild.transformNormalDoorState(segPoint, rotate, borderChild.rotateState, borderChild.segFlag);
										borderChild.x = Number(borderChildXML.@x);
										borderChild.y = Number(borderChildXML.@y);
										borderChild.parentBorder = currentBorder2;	
										var absordRoom:AbstractDrawingRoom = gridContainer.getChildAt(int(borderChildXML.@roomIndex)) as AbstractDrawingRoom;

										/*var dataString1:String = borderChildXML.@data.toString();				
										var dataArray1:Array = dataString1.split(",", dataString1.length) as Array;				
										borderChild.data = new Vector.<Number>();										
										for(var m2:int = 0; m2 < dataArray1.length; m2++)
										{
										borderChild.data.push(dataArray1[m2]);
										}*/
										
										
										// 整理顶点 data 数据
										/*var dataString1:String = borderChildXML.@data.toString();				
										var dataArray1:Array = dataString1.split(",", dataString1.length) as Array;				
										borderChild.data = new Vector.<Number>();										
										for(var m2:int = 0; m2 < dataArray1.length; m2++)
										{
										borderChild.data.push(dataArray1[m2]);
										}*/
										
										/*var segPoint:Point = MathCalculation.getOffsetValue(currentBorder2.k, borderChild.length, currentBorder2.data);
										var rotate:Number = borderChild.getReverseRotation(borderChild.data, currentBorder2.k);
										borderChild.transformNormalDoorState(segPoint, rotate, borderChild.rotateState, borderChild.segFlag);*/
										
										// 必须手动设置 父边框，否则无法响应鼠标事件
									}
									else if (borderChildXML.@type.toString() == "openingWall")
									{
										
										borderChild = new OpeningWall(Number(borderChildXML.@length));
										
										currentBorder2.addChild(borderChild);
										//borderArray[borderArr[0]].borders[borderArr[1]].addChild(borderChild);
										//tempBorder.addChild(borderChild);
										borderChild.x = Number(borderChildXML.@x);
										borderChild.y = Number(borderChildXML.@y);
										borderChild.rotation = Number(borderChildXML.@rotation);
										borderChild.percentPosition = Number(borderChildXML.@percentPosition);
										
										var wallString:String = borderChildXML.@data.toString();				
										var wallArray:Array = wallString.split(",", wallString.length) as Array;				
										borderChild.data = new Vector.<Number>();										
										for(var o:int = 0; o < wallArray.length; o++)
										{
											borderChild.data.push(wallArray[o]);
										}
										
										// 必须手动设置 父边框，否则无法响应鼠标事件
										//borderChild.parentBorder = borderArray[borderArr[0]].borders[borderArr[1]];
										//borderChild.parentBorder = tempBorder;
										borderChild.parentBorder = currentBorder;
									}
									else if (borderChildXML.@type.toString() == "absorbRelatedBorder")
									{
										var absordRoom2:AbstractDrawingRoom = gridContainer.getChildAt(int(borderChildXML.@roomIndex)) as AbstractDrawingRoom;
										for (var t6:int = 0; t6 <absordRoom2.numChildren; t6++)
										{
											if(absordRoom2.getChildAt(t6) is RoomBorder && (absordRoom2.getChildAt(t6) as RoomBorder).index == int(borderChildXML.@absorbIndex))
											{
												currentBorder2.absorbRelatedBorder.push(absordRoom2.getChildAt(t6) as RoomBorder);		
											}
										}										
									}
								}
							}
						}
					}
				}
			}
			
			/*	for (var h:int = 0; h < roomBorderLength; h++)
			{
			var borderXML:XML = currentRoomXML.roomBorder[h];
			var tempBorder:RoomBorder = roomBorder;															
			
			borders.addItem(tempBorder);
			for (var m:int = 0; m < borderXML.children().length(); m++)
			{
			// 算法示意
			// 判断到底是 门，还是窗，还是开放墙
			// 门，窗 XML 需要添加 类型属性（单开还是双开等）、长度、Rotaion、data、percentPosition
			// 然后新建一个
			// 然后 addChild 到对应的 roomBorder 中
			// 设置 x，y
			var borderChildXML:XML = borderXML.children()[m];
			var borderChild:AbstractBorderChild;
			
			tempBorder.name = a.toString() + "," + h.toString() +  "," + k.toString();
			
			if(borderChildXML.@type.toString() == "window")
			{							
			var windowHostGoods:Goods = goodsModel.getWindowGoodsByID(int(borderChildXML.@goodsID));
			var windowLoader:Loader = LoadAdmin.loadBitmapData(windowHostGoods.dragPicSrc);
			windowLoader.name = a.toString() + "," + h.toString() + "," + k.toString();
			windowLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadWindowCompleteHandler);
			function loadWindowCompleteHandler(event:Event):void
			{
			var windowString:String = event.target.loader.name;
			var windowArray:Array = windowString.split(",", windowString.length) as Array;																						
			trace(borderArray[windowArray[0]].borders.get(0));
			var windowXML:XML = roomXML.room[windowArray[0]]..roomBorder[windowArray[1]].children()[windowArray[2]];
			//var tempXML:XML = borderXML.children()[event.target.loader.name];
			if (windowXML.@typeIndex.toString() == "0")
			{
			borderChild = new FixedWindow(Number(windowXML.@length), event.target.content.bitmapData, windowHostGoods);
			}
			// 新建 DoubleWindow
			else if (windowXML.@typeIndex.toString() == "1")
			{
			borderChild = new DoubleWindow(Number(windowXML.@length), event.target.content.bitmapData, windowHostGoods);
			}
			else if (windowXML.@typeIndex.toString() == "2")
			{
			borderChild = new CommonWindow(Number(windowXML.@length), event.target.content.bitmapData, windowHostGoods);
			}
			
			var currentBorder1:RoomBorder;
			for each(var border1:RoomBorder in borderArray[windowArray[0]].borders)
			{
			
			if(border1.index == int(windowXML.@parentIndex.toString()))
			{
			currentBorder1 = border1;											
			}
			}
			currentBorder1.addChild(borderChild);
			//borderArray[windowArray[0]].borders[windowArray[1]].addChild(borderChild);
			borderChild.x = Number(windowXML.@x);
			borderChild.y = Number(windowXML.@y);
			borderChild.rotation = Number(windowXML.@rotation);
			borderChild.percentPosition = Number(windowXML.@percentPosition);
			
			// 整理顶点 data 数据
			var dataString:String = windowXML.@data.toString();				
			var dataArray:Array = dataString.split(",", dataString.length) as Array;				
			borderChild.data = new Vector.<Number>();										
			for(var m:int = 0; m < dataArray.length; m++)
			{
			borderChild.data.push(dataArray[m]);
			}
			
			// 必须手动设置 父边框，否则无法响应鼠标事件
			//borderChild.parentBorder = borderArray[windowArray[0]].borders[windowArray[1]];
			borderChild.parentBorder = currentBorder1;
			
			}
			// 新建 FixedWindow
			/*if (borderChildXML.@index.toString() == "0")
			{
			borderChild = new FixedWindow(Number(borderChildXML.@length), null, windowHostGoods);
			}
			// 新建 DoubleWindow
			else if (borderChildXML.@index.toString() == "1")
			{
			borderChild = new DoubleWindow(Number(borderChildXML.@length), null, windowHostGoods);
			}
			}*/
			/*else if(borderChildXML.@type.toString() == "door")
			{							
			var doorHostGoods:Goods = goodsModel.getDoorGoodsByID(int(borderChildXML.@goodsID));
			
			var doorLoader:Loader = LoadAdmin.loadBitmapData(doorHostGoods.dragPicSrc);
			doorLoader.name = a.toString() + "," + h.toString() + "," + k.toString();
			trace("d"+doorLoader.name.toString());
			doorLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadDoorCompleteHandler);
			
			function loadDoorCompleteHandler(event:Event):void
			{
			var nameString:String = event.target.loader.name;				
			var nameArray:Array = nameString.split(",", nameString.length) as Array;																						
			
			var tempXML2:XML = roomXML.room[nameArray[0]]..roomBorder[nameArray[1]].children()[nameArray[2]];
			//var tempXML2:XML = tempXML1.children()[nameArray[2]];
			roomXML.room[0]..roomBorder[2];
			
			if (tempXML2.@typeIndex.toString() == "0")
			{
			borderChild = new SingleDoor(Number(tempXML2.@length),  event.target.content.bitmapData, doorHostGoods);
			}
			// 新建 ShiftDoor
			else if (tempXML2.@typeIndex.toString() == "1")
			{
			borderChild = new ShiftDoor(Number(tempXML2.@length),  event.target.content.bitmapData, doorHostGoods);
			}
			// 新建 DoubleShiftDoor
			else if (tempXML2.@typeIndex.toString() == "2")
			{
			borderChild = new DoubleShiftDoor(Number(tempXML2.@length),  event.target.content.bitmapData, doorHostGoods);
			}
			// 新建 DoubleDoor
			else if (tempXML2.@typeIndex.toString() == "3")
			{
			borderChild = new DoubleDoor(Number(tempXML2.@length),  event.target.content.bitmapData, doorHostGoods);
			}
			//borders[nameArray[1]].addChild(borderChild);
			//tempBorder.addChild(borderChild);
			
			var currentBorder2:RoomBorder;
			for each(var border2:RoomBorder in borderArray[nameArray[0]].borders)
			{
			
			if(border2.index == int(tempXML2.@parentIndex.toString()))
			{
			currentBorder2 = border2;
			
			}
			}
			
			
			currentBorder2.addChild(borderChild);
			//borderArray[nameArray[0]].borders[nameArray[1]].addChild(borderChild);
			borderChild.x = Number(tempXML2.@x);
			borderChild.y = Number(tempXML2.@y);
			borderChild.rotation = Number(tempXML2.@rotation);
			borderChild.percentPosition = Number(tempXML2.@percentPosition);
			borderChild.shared = Boolean(tempXML2.@shared);
			if(borderChild.shared)
			aborBorder = currentBorder2;
			
			// 整理顶点 data 数据
			var dataString:String = tempXML2.@data.toString();				
			var dataArray:Array = dataString.split(",", dataString.length) as Array;				
			borderChild.data = new Vector.<Number>();										
			for(var m:int = 0; m < dataArray.length; m++)
			{
			borderChild.data.push(dataArray[m]);
			}
			
			// 必须手动设置 父边框，否则无法响应鼠标事件
			//borderChild.parentBorder = borderArray[nameArray[0]].borders[nameArray[1]];
			//tempBorder.addChild(borderChild);
			//borderChild.parentBorder = tempBorder;
			borderChild.parentBorder = currentBorder2;
			}								
			}
			else if (borderChildXML.@type.toString() == "openingWall")
			{
			
			borderChild = new OpeningWall(Number(borderChildXML.@length));
			//trace(tempBorder.data);
			//borderArray[0].borders[2].data;
			var currentBorder:RoomBorder;
			for each(var border:RoomBorder in borderArray[a].borders)
			{
			
			if(border.index == int(borderChildXML.@parentIndex.toString()))
			{
			currentBorder = border;									
			}
			}
			currentBorder.addChild(borderChild);
			//borderArray[borderArr[0]].borders[borderArr[1]].addChild(borderChild);
			tempBorder.addChild(borderChild);
			borderChild.x = Number(borderChildXML.@x);
			borderChild.y = Number(borderChildXML.@y);
			borderChild.rotation = Number(borderChildXML.@rotation);
			borderChild.percentPosition = Number(borderChildXML.@percentPosition);
			
			var wallString:String = borderChildXML.@data.toString();				
			var wallArray:Array = wallString.split(",", wallString.length) as Array;				
			borderChild.data = new Vector.<Number>();										
			for(var o:int = 0; o < wallArray.length; o++)
			{
			borderChild.data.push(wallArray[o]);
			}
			
			// 必须手动设置 父边框，否则无法响应鼠标事件
			//borderChild.parentBorder = borderArray[borderArr[0]].borders[borderArr[1]];
			//borderChild.parentBorder = tempBorder;
			borderChild.parentBorder = currentBorder;
			}
			}
			// 寻找下一个 RoomBorder
			while (currentNode.next != room.borderVertexList.head)
			{
			currentNode = currentNode.next;
			if (currentNode.data is RoomBorder)
			{
			roomBorder = currentNode.data as RoomBorder;
			break;
			}
			}		
			}*/
		}
		
	}
}